﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using MarginTrading.Entity;
using MarginTrading.Common;
using System.Data.OracleClient;
using System.Data;

namespace MarginTrading.Services
{
    public class CDSTranServices
    {
        /*
        private static Agent GetAgentFromDataReader(IDataReader dataReader)
        {
            Agent retVal = null;
            if (dataReader != null && !dataReader.IsClosed)
            {
                retVal = new Agent();
                retVal.Code = dataReader["CODE"].ToString();
                retVal.Name = dataReader["NAME"].ToString();
                retVal.Address_1 = dataReader["ADDRESS_1"].ToString();
                retVal.Address_2 = dataReader["ADDRESS_2"].ToString();
                retVal.Address_3 = dataReader["ADDRESS_3"].ToString();
                retVal.Address_4 = dataReader["ADDRESS_4"].ToString();
                retVal.Telephone1 = dataReader["TELEPHONE_1"].ToString();
                retVal.Telephone2 = dataReader["TELEPHONE_2"].ToString();
                retVal.Fax = dataReader["FAX"].ToString();
                retVal.Interest_Rate = double.Parse(dataReader["INTEREST_RATE"].ToString());
                DateTime createdDate = DateTime.MinValue;
                if (DateTime.TryParse(dataReader["CREATED_DATE"].ToString(), out createdDate))
                    retVal.CreatedDate = createdDate;
            }
            return retVal;
        }
        
        */


        public static List<string> GetCDSTranCollectionDataTable(DateTime TrDate)
        {
            List<string> retVal = new List<string>();
            OracleConnection con = new OracleConnection();


            try
            {
                con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                con.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.CommandText = "select REFERENCE_NO from CDS_TRAN where TRANSACTION_DATE = :v1";

                cmd.Parameters.Add(":v1", OracleType.DateTime).Value = TrDate;

                cmd.Connection = con;

                using (IDataReader dataReader = cmd.ExecuteReader())
                {
                    if (dataReader != null)
                    {
                        while (dataReader.Read())
                        {
                            string item = dataReader["REFERENCE_NO"].ToString();
                            if (!string.IsNullOrEmpty(item))
                                retVal.Add(item);
                        }
                       
                    }
                }
            }
            catch (System.Exception exc)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
            return retVal;
        }

        public static void Add(CDSTran trans)
        {
            OracleConnection con = new OracleConnection();

            try
            {
                con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                con.Open();
                OracleCommand cmd = new OracleCommand();
                cmd.CommandText = "insert into CDS_TRAN(TRANSACTION_DATE,REFERENCE_NO, COMPANY_SYMBOL,CLIENT_CODE,TRANSACTION_TYPE," +
                                  "TRANSACTION_DESC,QUANTITY,TRADE_DATE,BROKER_CODE)" +
                                  "values(:v1,:v2,:v3,:v4,:v5,:v6,:v7,:v8,:v9)";

                cmd.Parameters.Add(":v1", OracleType.DateTime).Value = trans.transaction_date;
                cmd.Parameters.Add(":v2", OracleType.VarChar).Value = trans.reference;
                cmd.Parameters.Add(":v3", OracleType.VarChar).Value = trans.company_symbol;
                cmd.Parameters.Add(":v4", OracleType.VarChar).Value = trans.client_code;
                cmd.Parameters.Add(":v5", OracleType.VarChar).Value = trans.transaction_type;
                cmd.Parameters.Add(":v6", OracleType.VarChar).Value = trans.transaction_desc;
                cmd.Parameters.Add(":v7", OracleType.Number).Value = trans.quantity;
                cmd.Parameters.Add(":v8", OracleType.VarChar).Value = trans.trade_date;
                cmd.Parameters.Add(":v9", OracleType.VarChar).Value = trans.broker_code;
               

                cmd.Connection = con;
                cmd.ExecuteNonQuery();

            }
            catch (System.Exception exc)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }

        
    }
}
